package demo10_flyweight.util;

import demo10_flyweight.model.Material;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class FlyweightFactory {
    private static Map<Integer,Material> materialMap = new HashMap<>(16);

    public static Material addMaterial(Material material) {
        //模拟获取素材的MD5值
        Integer fileMd5 = Md5Util.getFileMd5(material);
        Material result = Optional.ofNullable(materialMap.get(fileMd5)).orElseGet(() -> {
            materialMap.put(fileMd5, material);
            return material;
        });
        return result;
    }


    public static int size() {
        return materialMap.size();
    }


}
